package cn.iocoder.yudao.module.trade.controller.admin.fruitcard;

import cn.iocoder.yudao.module.trade.controller.admin.fruitcard.vo.*;
import cn.iocoder.yudao.module.trade.convert.fruitcard.FruitCardConvert;
import cn.iocoder.yudao.module.trade.dal.mysql.fruitcard.FruitCardDO;
import cn.iocoder.yudao.module.trade.service.fruitcard.FruitCardService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 水果卡")
@RestController
@RequestMapping("/fruit/card")
@Validated
public class FruitCardController {

    @Resource
    private FruitCardService cardService;

    @PostMapping("/create")
    @Operation(summary = "创建水果卡")
    @PreAuthorize("@ss.hasPermission('fruit:card:create')")
    public CommonResult<Long> createCard(@Valid @RequestBody FruitCardCreateReqVO createReqVO) {
        return success(cardService.createCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水果卡")
    @PreAuthorize("@ss.hasPermission('fruit:card:update')")
    public CommonResult<Boolean> updateCard(@Valid @RequestBody FruitCardUpdateReqVO updateReqVO) {
        cardService.updateCard(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新水果卡状态")
    @PreAuthorize("@ss.hasPermission('fruit:card:update')")
    public CommonResult<Boolean> updateCardStatus(@Valid @RequestBody FruitCardUpdateStatusReqVO updateReqVO) {
        cardService.updateCardStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水果卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fruit:card:delete')")
    public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
        cardService.deleteCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水果卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fruit:card:query')")
    public CommonResult<FruitCardRespVO> getCard(@RequestParam("id") Long id) {
        FruitCardDO card = cardService.getCard(id);
        return success(FruitCardConvert.INSTANCE.convert(card));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水果卡列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fruit:card:query')")
    public CommonResult<List<FruitCardRespVO>> getCardList(@RequestParam("ids") Collection<Long> ids) {
        List<FruitCardDO> list = cardService.getCardList(ids);
        return success(FruitCardConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水果卡分页")
    @PreAuthorize("@ss.hasPermission('fruit:card:query')")
    public CommonResult<PageResult<FruitCardRespVO>> getCardPage(@Valid FruitCardPageReqVO pageVO) {

        return success(cardService.getCardPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水果卡 Excel")
    @PreAuthorize("@ss.hasPermission('fruit:card:export')")
    @OperateLog(type = EXPORT)
    public void exportCardExcel(@Valid FruitCardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FruitCardDO> list = cardService.getCardList(exportReqVO);
        // 导出 Excel
        List<FruitCardExcelVO> datas = FruitCardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水果卡.xls", "数据", FruitCardExcelVO.class, datas);
    }

    @PostMapping("/init-batch")
    @Operation(summary = "批量初始化水果卡")
    @PreAuthorize("@ss.hasPermission('fruit:card:create')")
    public CommonResult<Boolean> initBatch(@Valid @RequestBody FruitCardInitBatchReqVO vo) {
        cardService.initBatch(vo);
        return success(Boolean.TRUE);
    }




}
